package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.supplier.SupplierApplicationService;
import com.agileboot.attendance.domain.supplier.command.AddSupplierCommand;
import com.agileboot.attendance.domain.supplier.command.UpdateSupplierCommand;
import com.agileboot.attendance.domain.supplier.dto.SearchSupplierDTO;
import com.agileboot.attendance.domain.supplier.dto.SupplierDTO;
import com.agileboot.attendance.domain.supplier.dto.SupplierOptionDTO;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.core.page.PageQuery;
import com.agileboot.common.core.command.BulkOperationCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * <p>
 * 供应商管理 前端控制器
 * </p>
 *
 * @author agileboot
 * @since 2023-03-14
 */
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "供应商管理接口")
@RequestMapping("/attendance/supplier")
public class SupplierController extends BaseController {

    private final SupplierApplicationService supplierApplicationService;

    /**
     * 获取供应商列表
     */
    @ApiOperation("获取供应商列表")
    @PreAuthorize("@permission.has('attendance:supplier:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<SupplierDTO>> list(SearchSupplierDTO searchSupplierDTO, PageQuery pageQuery) {
        pageQuery.setParam(searchSupplierDTO);
        PageDTO<SupplierDTO> pageDTO = supplierApplicationService.getSupplierList(pageQuery);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 获取供应商详情
     */
    @ApiOperation("获取供应商详情")
    @PreAuthorize("@permission.has('attendance:supplier:query')")
    @GetMapping("/{supplierId}")
    public ResponseDTO<SupplierDTO> getInfo(@PathVariable Long supplierId) {
        SupplierDTO supplierDTO = supplierApplicationService.getSupplierInfo(supplierId);
        return ResponseDTO.ok(supplierDTO);
    }

    /**
     * 新增供应商
     */
    @ApiOperation("新增供应商")
    @PreAuthorize("@permission.has('attendance:supplier:add')")
    @PostMapping
    public ResponseDTO<Void> add(@Valid @RequestBody AddSupplierCommand addSupplierCommand) {
        supplierApplicationService.addSupplier(addSupplierCommand);
        return ResponseDTO.ok();
    }

    /**
     * 修改供应商
     */
    @ApiOperation("修改供应商")
    @PreAuthorize("@permission.has('attendance:supplier:edit')")
    @PutMapping
    public ResponseDTO<Void> edit(@Valid @RequestBody UpdateSupplierCommand updateSupplierCommand) {
        supplierApplicationService.updateSupplier(updateSupplierCommand);
        return ResponseDTO.ok();
    }

    /**
     * 删除供应商
     */
    @ApiOperation("删除供应商")
    @PreAuthorize("@permission.has('attendance:supplier:remove')")
    @DeleteMapping
    public ResponseDTO<Void> remove(@Valid @RequestBody BulkOperationCommand<Long> deleteSupplierCommand) {
        supplierApplicationService.deleteSupplier(deleteSupplierCommand);
        return ResponseDTO.ok();
    }
    
    /**
     * 导出供应商列表
     */
    @ApiOperation("导出供应商列表")
    @PreAuthorize("@permission.has('attendance:supplier:export')")
    @GetMapping("/export")
    public void export(SearchSupplierDTO searchSupplierDTO, HttpServletResponse response) {
        supplierApplicationService.exportSupplier(searchSupplierDTO, response);
    }
    
    /**
     * 导入供应商
     */
    @ApiOperation("导入供应商")
    @PreAuthorize("@permission.has('attendance:supplier:import')")
    @PostMapping("/import")
    public ResponseDTO<Void> importSupplier(MultipartFile file) {
        supplierApplicationService.importSupplier(file);
        return ResponseDTO.ok();
    }
    
    /**
     * 下载导入模板
     */
    @ApiOperation("下载导入模板")
    @PreAuthorize("@permission.has('attendance:supplier:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        supplierApplicationService.downloadImportTemplate(response);
    }
    
    /**
     * 获取供应商选项列表
     */
    @ApiOperation("获取供应商选项列表")
    @GetMapping("/options")
    public ResponseDTO<List<SupplierOptionDTO>> getOptions() {
        List<SupplierOptionDTO> supplierOptions = supplierApplicationService.getSupplierOptions();
        return ResponseDTO.ok(supplierOptions);
    }

    /**
     * 获取指定项目的供应商列表
     */
    @ApiOperation("获取指定项目的供应商列表")
    @PreAuthorize("@permission.has('attendance:supplier:list')")
    @GetMapping("/list/project")
    public ResponseDTO<PageDTO<SupplierDTO>> listByProject(Long projectId, Integer pageNum, Integer pageSize) {
        // 创建一个自定义的PageQuery
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        
        // 调用服务层方法获取项目关联的供应商列表
        PageDTO<SupplierDTO> pageDTO = supplierApplicationService.getSupplierListByProjectId(projectId, pageQuery);
        return ResponseDTO.ok(pageDTO);
    }
} 